import { toast, loading } from './utils/util'
import { store } from './store/index'
import _login from './api/login'
import { _addshare } from './api/projects'
import { _getUserInfoByUid, _getWXUserInfo } from '/api/getuserinfo'
App({
  shareInfo: {
    title: '阳光城欢迎您！'
  },
  globalData: {
    memberInfo: null
  },
  toast,
  loading,
  checkAuth() {
    let uid = wx.getStorageSync('uid')
    return new Promise((resolve, reject) => {
      if (uid) {
        resolve(uid)
      } else {
        // 登录获取uid
        wx.login({
          success: r => {
            if (r.code) {
              _login(r.code)
                .then(res => {
                  if (res.data.code === 0) {
                    console.log(res.data.data)
                    let {
                      OpenId: openid,
                      SessionKey: session_key,
                      UnionId: unionid
                    } = res.data.data
                    unionid = unionid || wx.getStorageSync('uid')
                    if (unionid) {
                      resolve(unionid)
                    } else {
                      reject('登录没有uid')
                    }
                    wx.setStorageSync('openid', openid)
                    wx.setStorageSync('uid', unionid)
                    wx.setStorageSync('s_key', session_key)
                  } else {
                    reject(res.data.msg)
                  }
                })
                .catch(err => {
                  reject(err)
                })
            }
          }
        })
      }
    })
  },
  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success: r => {
          if (r.code) {
            _login(r.code)
              .then(res => {
                const { code, msg, data } = res.data
                if (code === 0) {
                  console.log(data)
                  let { OpenId, SessionKey, UnionId } = data
                  UnionId = UnionId || wx.getStorageSync('uid')
                  resolve({ OpenId, SessionKey, UnionId })
                  wx.setStorageSync('openid', OpenId)
                  wx.setStorageSync('uid', UnionId)
                  wx.setStorageSync('s_key', SessionKey)
                } else {
                  reject(res.data)
                }
              })
              .catch(err => {
                reject(err)
              })
          }
        }
      })
    })
  },
  getUserInfoByUid(uid, force = false) {
    const memberInfo = wx.getStorageSync('member')
    return new Promise((resolve, reject) => {
      // force || !memberInfo
      if (1) {
        _getUserInfoByUid(uid)
          .then(res => {
            if (res.data.code === 0) {
              this.globalData.memberInfo = res.data.data
              wx.setStorageSync('member', res.data.data)
              store.updateMember(res.data.data)
              resolve(res.data.data)
            } else {
              reject(res.data)
              wx.removeStorageSync('member')
              store.updateMember(null)
            }
          })
          .catch(err => {
            reject(err)
            wx.removeStorageSync('member')
            store.updateMember(null)
          })
      } else {
        resolve(memberInfo)
      }
    })
  },
  onGetWXUserInfo(e) {
    const { iv, encryptedData } = e.detail
    return new Promise((resolve, reject) => {
      if (iv) {
        loading('加载中')
        const OpenId = wx.getStorageSync('openid')
        _getWXUserInfo({ OpenId, iv, encryptedData })
          .then(res => {
            wx.hideLoading()
            const { code, data, msg } = res.data
            if (code === 0) {
              const { Fans, User } = data
              const member = Fans[0]
              let { openId, unionId, avatarUrl, nickName } = User
              unionId && wx.setStorageSync('uid', unionId)
              member && wx.setStorageSync('member', member)
              resolve({ unionId, member })
            } else {
              wx.showModal({
                title: '对不起',
                content: msg,
                showCancel: false
              })
            }
          })
          .catch(err => {
            console.log(err)
            wx.hideLoading()
            wx.showModal({
              title: '对不起',
              content: JSON.stringify(err),
              showCancel: false
            })
          })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: '您已拒绝授权',
          showCancel: false
        })
      }
    })
  },
  checkMember () {
    let member = wx.getStorageSync('member')
    return new Promise((resolve, reject) => {
      if (member) {
        resolve(member)
      } else {
        reject('本地缓存无member')
      }
    })
  },
  addShare (opt) {
    const { id: ObjectID, type: Type } = opt
    const ShareUnionID = wx.getStorageSync('shareuid')
    const UnionID = wx.getStorageSync('uid')
    console.log(ShareUnionID, UnionID, ObjectID, Type)
    if (!ObjectID || !Type || !UnionID || !ShareUnionID) {
      return
    }
    console.log('授权后增加分享')
    _addshare({ ShareUnionID, UnionID, ObjectID, Type })
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  },
  onLaunch() { }
})